package com.nari.ysphd.ui;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.jeremyfeinstein.gson.bean.QueryDeviceBean;
import com.jeremyfeinstein.utils.NetUtils.ApiResponse;
import com.jeremyfeinstein.utils.NetUtils.JsonCallback;
import com.jeremyfeinstein.utils.NetUtils.SGApiService;
import com.nari.ysphd.MyApp;
import com.nari.ysphd.R;
import com.nari.ysphd.adapter.MeetingListAdapter;
import com.nari.ysphd.fragment.ConcludedMeetingsFragment;
import com.nari.ysphd.fragment.ForthcomingMeetingsFragment;
import com.nari.ysphd.fragment.OngoingMeetingsFragment;
import com.nari.ysphd.util.log.HDMsgUtils;
import com.orhanobut.logger.Logger;

import java.util.List;

import static com.nari.ysphd.util.log.HDMsgUtils.getDevCode;
import static com.nari.ysphd.util.log.HDMsgUtils.getDevName;

/**
 * Create by sunxunchao on 2019/3/29
 */
public class MeetingListActivity extends BaseActivity {


    private static final String TAG = "MeetingListActivity";
    private ImageView back;
    private ImageView iv_message;
    private ImageView iv_meeting_manage;
    private ImageView iv_search;
    private TabLayout meeting_list_tab;
    private ViewPager meeting_list_viewpager;

    private String[] mTabTitles = {"正在进行", "即将开始", "历史互动"};
    private Fragment[] fragments = new Fragment[3];
    private MeetingListAdapter adapter;
    private AlertDialog loginOutDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_meeting_list);
        MyApp.isLoginOut = false;
        back = (ImageView) findViewById(R.id.back);
        iv_message = (ImageView) findViewById(R.id.iv_message);
        iv_meeting_manage = (ImageView) findViewById(R.id.iv_meeting_manage);
        iv_search = (ImageView) findViewById(R.id.iv_search);
        meeting_list_tab = (TabLayout) findViewById(R.id.meeting_list_tab);
        meeting_list_viewpager = (ViewPager) findViewById(R.id.meeting_list_viewpager);

        fragments[0] = OngoingMeetingsFragment.newInstance("");
        fragments[1] = ForthcomingMeetingsFragment.newInstance("");
        fragments[2] = ConcludedMeetingsFragment.newInstance("");

        meeting_list_tab.setTabMode(TabLayout.MODE_FIXED);
        meeting_list_tab.setTabGravity(TabLayout.GRAVITY_FILL);
        adapter = new MeetingListAdapter(getSupportFragmentManager(), fragments, mTabTitles);
        meeting_list_viewpager.setAdapter(adapter);
        meeting_list_viewpager.setOffscreenPageLimit(2);
        // 将ViewPager和TabLayout绑定
        meeting_list_tab.setupWithViewPager(meeting_list_viewpager);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logoutDialog();
            }
        });

        iv_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MeetingListActivity.this, UserActivity.class));
            }
        });

        iv_meeting_manage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MeetingListActivity.this, MeetingManageUI.class);
                startActivity(intent);
            }
        });

        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Log.d(TAG, "meeting page: " + meeting_list_viewpager.getCurrentItem());
                MeetingSearchActivity.startActivity(MeetingListActivity.this, meeting_list_viewpager.getCurrentItem());
            }
        });
        initDevInfo();
    }

    public void initDevInfo() {
        final String devCode = getDevCode();
        final String devName = getDevName();
        HDMsgUtils.getInstance().init();
        SGApiService.postJSON("queryDevice")
                .addParam("devName", devName)
                .addParam("devStatus", "")
                .addParam("authorizationStatus", "")
                .addParam("pageNo", "1")
                .addParam("pageSize", "10")
                .post(new JsonCallback<QueryDeviceBean>() {
                    @Override
                    public void onSuccess(ApiResponse<QueryDeviceBean> response) {
                        super.onSuccess(response);
                        QueryDeviceBean body = response.body;
                        if (body.isSuccessful() && "200".equals(body.getResultCode())) {
                            List<QueryDeviceBean.ResultValueBean> resultValue = body.getResultValue();
                            if (resultValue != null && resultValue.size() == 0) {
                                Log.e(TAG, "onSuccess: -----------");
                                SGApiService.postJSON("addDevice")
                                        .addParam("devName", devName)
                                        .addParam("devCode", devCode)
                                        .post(getCallback());
                            }
                        }
                    }

                    @Override
                    public void onError(ApiResponse<QueryDeviceBean> response) {
                        super.onError(response);
                        Log.e(TAG, "onError: response.message " + response.message);
                    }
                });
    }

    private JsonCallback<String> getCallback() {
        return new JsonCallback<String>() {
            @Override
            public void onSuccess(ApiResponse<String> response) {
                super.onSuccess(response);
                Logger.e("Response  %s", "onSuccess: " + response.body);
            }

            @Override
            public void onError(ApiResponse<String> response) {
                super.onError(response);
                Logger.e("Response  %s", "onError: " + response.message);
            }
        };
    }


    @Override
    public void onBackPressed() {
        logoutDialog();
    }

    private void logoutDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MeetingListActivity.this);
        builder.setMessage("是否要退出登录？");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
                dialog.dismiss();
            }
        });
        loginOutDialog = builder.create();
        loginOutDialog.show();
    }

//    protected void logout() {
////        YCHSWebSocketAPI.getApp().closeSocket();
//        YCHSInstance.resetInstance();
//        YCHShttpAPI.LogOut(MeetingListActivity.this,
//                new StringCallback() {
//                    @Override
//                    public void onResponse(String response) {
//                        /*Intent intent = new Intent(MeetingListActivity.this, LoginUI.class);
//                        startActivity(intent);
//                        finish();*/
//                        jumpToMipLogin();
//
//
//                    }
//
//                    @Override
//                    public void onError(Call call, int statusCode, Exception e) {
//                        Log.e(TAG, "onError: " + e.getMessage());
//                        jumpToMipLogin();
//
//                    }
//                });
//
//    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy: ");
        super.onDestroy();
        if (loginOutDialog != null && loginOutDialog.isShowing()) {
            loginOutDialog.dismiss();
        }
    }


}
